package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Order;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

public class ConfirmOrderServlet extends HttpServlet {
    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/ShippingForm.jsp";
    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Order order =(Order)req.getSession().getAttribute("order");


        String username = ((Account)req.getSession().getAttribute("account")).getUsername();
        Date orderDate = new Date(new java.util.Date().getTime());//获取当前时间
        String billAddress1 = req.getParameter("billAddress1");
        String billAddress2 = req.getParameter("billAddress2");
        String billCity = req.getParameter("billCity");
        String billState = req.getParameter("billState");
        String billZip = req.getParameter("billZip");
        String billCountry = req.getParameter("billCountry");
        String creditCard = req.getParameter("creditCard");
        String expiryDate = req.getParameter("expiryDate");
        String billToFirstName = req.getParameter("billToFirstName");
        String billToLastName = req.getParameter("billToLastName");


        order.setUsername(username);
        order.setOrderDate(orderDate);

        order.setBillAddress1(billAddress1);
        order.setBillAddress2(billAddress2);
        order.setBillCity(billCity);
        order.setBillState(billState);
        order.setBillZip(billZip);
        order.setBillCountry(billCountry);
        order.setCourier("john");

        order.setBillToFirstName(billToFirstName);
        order.setBillToLastName(billToLastName);
        order.setCreditCard(creditCard);
        order.setExpiryDate(expiryDate);
        order.setCardType("Wechat Pay");
        order.setLocale("Chang Sha");

        order.setStatus("0");//设置为未支付状态

        if(req.getParameterValues("shippingAddressRequired") != null){//查看复选框是否被选中
            if(req.getParameter("shippingFormFilled") == null){//Shipping表单没有被填写
                req.getRequestDispatcher(SHIPPING_FORM).forward(req, resp);//请求转发
                return;
            }
        }

        req.getRequestDispatcher(CONFIRM_ORDER).forward(req, resp);//跳转到确认页面
    }
}
